package com.empire.module.sporttery.vo;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.empire.framework.common.util.object.BeanUtils;
import com.empire.module.sporttery.controller.admin.footballmatch.vo.HalfScoreBallVo;
import com.empire.module.sporttery.controller.app.football.vo.AppFootballMatchRespVO;
import com.empire.module.sporttery.dal.dataobject.footballmatch.FootballMatchDO;
import com.empire.module.sporttery.util.SportteryUtils;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 足球竞猜 每天命中统计
 *
 * @author 仙人
 * @since 2025-10-05
 */
@Data
@Accessors(chain = true)
public class FootballDayResultVO implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 数据库中的字段数据格式：2025-3-17 星期一
     */
    private String startTime;
    /**
     * 日期：yyyy-mm-dd
     */
    private LocalDate day;
    /**
     * 日期:mm-dd
     */
    private String dayShow;
    /**
     * 方便排序
     */
    private Long dayTime;
    /**
     * 周几
     */
    private String week;
    /**
     * 命中场次
     */
    private Integer shoot = 0;

    /**
     * 比赛数量
     */
    private Integer selectCount = 0;

    /**
     * 赛事场数
     */
    private Integer matchCount = 0;
    /**
     * 当天的比赛场次
     */
    private List<AppFootballMatchRespVO> matchList;

    /**
     * 分析比赛场
     *
     * @param matchDOList 分析比赛场
     */
    public void seMatchDoList(List<FootballMatchDO> matchDOList) {
        this.setMatchList(BeanUtils.toBean(matchDOList, AppFootballMatchRespVO.class));
        if (matchList != null && !matchList.isEmpty()) {
            for (AppFootballMatchRespVO footballMatchVO : matchList) {
                if (StrUtil.isNotEmpty(footballMatchVO.getHalfFullCourt())) {//(0:0),0:2
                    String[] scores = footballMatchVO.getHalfFullCourt().split(",");
                    if (scores.length == 2) {
                        //转换一下，全场比分在前面，半场比分在后面
                        footballMatchVO.setHalfFullCourt(scores[0] + " " + scores[1]);
                    }
                } else {
                    if (StrUtil.isNotEmpty(footballMatchVO.getScore())) {
                        footballMatchVO.setHalfFullCourt(footballMatchVO.getScore());
                    } else {
                        footballMatchVO.setHalfFullCourt("VS");
                    }
                }
                if (StrUtil.isNotEmpty(footballMatchVO.getHalfFullCourt())) {//(0:0),0:2
                    String[] scores = footballMatchVO.getHalfFullCourt().split(",");
                    if (scores.length == 2) {
                        //转换一下，全场比分在前面，半场比分在后面
                        footballMatchVO.setHalfFullCourt(scores[0] + " " + scores[1]);
                    }
                } else {
                    if (StrUtil.isNotEmpty(footballMatchVO.getScore())) {
                        footballMatchVO.setHalfFullCourt(footballMatchVO.getScore());
                    } else {
                        footballMatchVO.setHalfFullCourt("VS");
                    }
                }
                //半全场赔率
                List<HalfScoreBallVo> halfWholeOddLists = StrUtil.isNotEmpty(footballMatchVO.getHalfWholeOddList())?
                        JSONArray.parseArray(footballMatchVO.getHalfWholeOddList(), HalfScoreBallVo.class):new ArrayList<>();
                footballMatchVO.setHalfWholeOddLists(halfWholeOddLists);
                footballMatchVO.setHalfWholeOddList("");
                //比分赔率
                List<List<HalfScoreBallVo>> scoreOddLists = new ArrayList<>();
                if (StringUtils.isNotEmpty(footballMatchVO.getScoreOddList())) {
                    JSONArray outerArray = JSON.parseArray(footballMatchVO.getScoreOddList());
                    for (int i = 0; i < outerArray.size(); i++) {
                        if(outerArray.get(i) instanceof JSONArray){
                            JSONArray innerArray = outerArray.getJSONArray(i);
                            List<HalfScoreBallVo> innerList = innerArray.toJavaList(HalfScoreBallVo.class);
                            scoreOddLists.add(innerList);
                        }
                    }
                    footballMatchVO.setScoreOddLists(scoreOddLists);
                }
                footballMatchVO.setScoreOddList("");
                //进球赔率
                List<HalfScoreBallVo> ballOddLists = StrUtil.isNotEmpty(footballMatchVO.getBallOddList())?
                        JSONArray.parseArray(footballMatchVO.getBallOddList(), HalfScoreBallVo.class):new ArrayList<>();
                footballMatchVO.setBallOddLists(ballOddLists);
                footballMatchVO.setBallOddList("");
            }
        }
    }

    public void setDay(LocalDate day) {
        this.day = day;
        LocalDateTime localDateTime = day.atStartOfDay(); // 转换为当天的开始时间（00:00:00）
        long timestamp = localDateTime.toEpochSecond(ZoneOffset.UTC); // 转换为时间戳（秒）
        this.setDayTime(timestamp);
        this.setDayShow(day.getMonthValue() + "-" + day.getDayOfMonth());
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
        //k里面有时间和星期，要分开
        String[] ks = startTime.split(" ");
        this.setDay(SportteryUtils.toLocalDateTime(ks[0]));//日期
        this.setWeek(ks[1].replace("星期", ""));//星期
    }
}
